<template>
	<el-dialog :title="title" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="选择取消原因" prop="">
				<div style="gap: 10px;row-gap: 10px;display: flex;flex-wrap: wrap;">
					<el-button style="margin-left: 0;" :type="form.cancel_reason==item ? 'primary': ''" plain @click="form.cancel_reason=item" v-for="(item, index) in tags" :key="index">{{item}}</el-button>
				</div>

			</el-form-item>
			<el-form-item v-if="form.cancel_reason=='其他原因'" label="原因" prop="cancel_reason">
				<el-input v-model="cancel_reason" clearable type="textarea"></el-input>
			</el-form-item>
			<el-form-item label="是否强制关闭" prop="is_force">
				<el-switch v-model="form.is_force" active-value="1" inactive-value="0"></el-switch>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				title: '取消订单',
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					id:"",
					cancel_reason: "",
					is_force: 0
				},
				cancel_reason: '',
				tags: [],
				//验证规则
				rules: {
					cancel_reason: [
						{required: true, message: '请输入关闭原因'}
					]
				}
			}
		},
		mounted() {

		},
		methods: {
			async getTags() {
				let res = await this.$API.order.express.closeReason.get({
				});
				if (res.code == 200) {
					this.tags = res.data
				} else {
					this.$alert(res.msg, "提示", {
						type: 'error'
					})
				}
			},
			//显示
			open(){
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						let form = JSON.parse(JSON.stringify(this.form))
						if (this.form.cancel_reason=='其他原因') {
							form['cancel_reason'] = this.cancel_reason
						}
						let res = await this.$API.order.express.cancel.post(this.form);
						delete form.cancel_reason
						this.isSaveing = false;
						if(res.code == 200){
							form['status'] = 6
							this.$emit('success', form, this.mode || 'edit')
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.msg, "提示", {type: 'error'})
						}
					}
				})
			},
			//表单注入数据
			setData(data, type){
				this.form.id = data.id
				if (this.tags.length == 0) this.getTags()
				if (type == 1) this.title = '退款'
				else this.title = '取消订单'
				// this.form.amount = data.amount
				// this.form.status = data.status
				// this.form.remark = data.remark

				//可以和上面一样单个注入，也可以像下面一样直接合并进去
				//Object.assign(this.form, data)
			}
		}
	}
</script>

<style>
</style>
